//
//  UIView+Category.m
//  RuntimeDemo
//
//  Created by 张敬 on 2019/10/14.
//  Copyright © 2019年 xiuxiu. All rights reserved.
//

#import "UIView+Category.h"
#import <objc/runtime.h>

static char const *const ObjectToKey = "ObjectToKey";

@implementation UIView (Category)

#pragma mark - setter and getter

- (void)setName:(NSString *)name{
    objc_setAssociatedObject(self, @selector(name), name, OBJC_ASSOCIATION_COPY);
}

- (NSString *)name{
    return objc_getAssociatedObject(self, @selector(name));
}

- (void)setTapBlock:(dealTapBlock)tapBlock{
    objc_setAssociatedObject(self, @selector(tapBlock), tapBlock, OBJC_ASSOCIATION_COPY);
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    [self addGestureRecognizer:tap];
}

- (dealTapBlock)tapBlock{
    return objc_getAssociatedObject(self, @selector(tapBlock));
}

- (void)setIsNeedAnimation:(BOOL)isNeedAnimation{
    NSNumber *number = [NSNumber numberWithBool:isNeedAnimation];
    objc_setAssociatedObject(self, ObjectToKey, number, OBJC_ASSOCIATION_ASSIGN);
}

- (BOOL)isNeedAnimation{
    NSNumber *number = objc_getAssociatedObject(self, ObjectToKey);
    return [number boolValue];
}

- (void)setDefaultColor:(UIColor *)defaultColor{
    objc_setAssociatedObject(self, @selector(defaultColor), defaultColor, OBJC_ASSOCIATION_RETAIN);
    self.backgroundColor = defaultColor;
}

- (UIColor *)defaultColor{
    return objc_getAssociatedObject(self, @selector(defaultColor));
}

#pragma mark - Other methods
- (void)tap{
    
    if (self.isNeedAnimation) {
        NSLog(@"点击我还需要些动画");
    }
    
    if (self.tapBlock) {
        self.tapBlock();
    }
}

@end
